package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.bean.Comment;
import com.demo.common.Result;
import com.demo.mapper.CommentMapper;
import com.demo.service.CommentService;
import com.demo.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
	@Autowired
	CommentMapper commentMapper;

	@Override
	public Result findById(Integer id) {
		if(-1 == id){
			QueryWrapper<Comment> wrapper = new QueryWrapper();

			return 	Result.ok().success(commentMapper.selectList(wrapper));
		}else {
			return Result.ok().success(commentMapper.selectById(id));
		}

	}

	@Override
	public Result findPage(Integer pageId, Integer pageSize) {
		if (null!=pageId && null!= pageSize) {
			Page<Comment> page = new Page<>(pageId,pageSize);
			IPage<Comment> iPage = commentMapper.selectArticlePage(page);
			return	Result.ok().success(iPage);
		}else {
			return	Result.ok().error("分页信息不可为空");
		}

	}

	@Override
	public Result insert(List<Comment> commentList) {
		if (null != commentList && commentList.size()>0){
			int i = commentMapper.add(commentList);
			if (i > 0) {
			return 	Result.ok("插入成功");
			} else {
				return Result.error("插入失败");
			}
		}else{
			return Result.error("传入信息不可为空");
		}

	}

	@Override
	public Result update(Comment comment) {
		if (null != comment && null != comment.getId() && !"".equals(comment.getId().toString())){
			int i = commentMapper.updateById(comment);
			if (i > 0) {
				return Result.ok("更新成功");
			} else {
				return Result.error("更新失败");
			}
		}else {
			return Result.error("更新信息不可为空");
		}
	}

	@Override
	public Result delete(List<Long> ids) {

		if (null!=ids && ids.size()>0){
			int i = commentMapper.deleteList(ids);
			if (i > 0) {
				return Result.ok("删除成功");
			} else {
				return Result.error("删除失败");
			}
		}else{
			return Result.error("id不可为空");
		}
	}

	@Override
	public Result commentVoPage(Integer pageId, Integer pageSize) {
		if (null!=pageId && null!= pageSize) {
			Page<CommentVo> page = new Page<>(pageId,pageSize);
			IPage<CommentVo> iPage = commentMapper.selectCommentVoPage(page);
			return Result.ok().success(iPage);
		}else {
			return Result.error("分页信息不可为空");
		}
	}
}
